package javase.javase02.networkprograming;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author: Gan
 * @date 2021/4/23 0023 19:44
 * @description:服务器端 1.客户端键盘录入，服务器输出文本文件
 **/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        // 创建ServerSocket对象，在指定端口，监听客户端连接请求
        ServerSocket serverSocket = new ServerSocket(3309);
        // 收到客户端连接请求后，建立Socket连接
        Socket socket = serverSocket.accept();
        // 从socket中根据需要获取输入流
        InputStream in = socket.getInputStream();
        //先读从客户端传过来的数据
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        //之后再写入指定文本当中
        BufferedWriter bw = new BufferedWriter(new FileWriter("log.txt"));
        //一行行读,再写入文本中
        String line;
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        bw.close();
        br.close();
        serverSocket.close();
    }
}
